package cn.learn.delay.config;

import cn.learn.delay.core.RedisDelayExecute;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : Yu.Tan
 * @date : 2022/10/11 18:12
 * @desc : **
 */
@Component
@Slf4j
public class ConsumerRunner implements ApplicationRunner {


    private static List<RedisDelayExecute> executes=new ArrayList<>();

    @Autowired
    ApplicationContext context;


    @PostConstruct
    public void initExecutes() {
        Map<String, RedisDelayExecute> executeMap = context.getBeansOfType(RedisDelayExecute.class);
        if (MapUtils.isNotEmpty(executeMap)){
            executes.addAll(executeMap.values());
        }
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        for (RedisDelayExecute execute : executes) {
            log.info("初始化 group={}，消费者启动！",execute.group());
            execute.consumer();
        }
    }
}
